{{Extend "layout"}}
{{Block "title"}}{{"采集规则"|T}}{{/Block}}
{{Block "breadcrumb"}}
{{Super}}
<li class="active">{{"采集规则"|T}}</li>
{{/Block}}
{{Block "main"}}
<div class="row">
	<div class="col-md-12">
		<div class="block-flat">
			<div class="header">
				<div class="pull-right" style="margin-left:10px">
					<form class="form-horizontal" action="" id="search-form" method="GET">
						<div class="input-group">
							<span class="input-group-addon">{{"分组"|T}}</span>
							<select name="groupId" class="form-control" id="groupId" onchange="$('#search-form').submit()">
								<option value="0"> - {{"不限"|T}} - </option>
								{{$groupId := Stored.groupId}} 
								{{range $k, $v := Stored.groupList}}
								<option value="{{$v.Id}}" {{if eq $groupId $v.Id}} selected{{end}}>{{$v.Name}}</option>
								{{end}}
							</select>
						</div>
					</form>
				</div>
				<a href="__BACKEND__/collector/rule_add" class="btn btn-success pull-right">
					<i class="fa fa-plus"></i>
					{{"添加规则"|T}}
				</a>
				<h3>{{"采集规则"|T}}</h3>
			</div>
			<div class="content">
				<div class="table-responsive">
				<table class="table no-border hover">
					<thead class="no-border">
						<tr>
							<th style="width:60px;"><strong>ID</strong></th>
							<th style="width:200px"><strong>{{"名称"|T}}</strong></th>
							<th style="width:100px"><strong>{{"浏览方式"|T}}</strong></th>
							<th style="width:100px"><strong>{{"分组"|T}}</strong></th>
							<th style="width:130px;"><strong>{{"创建时间"|T}}</strong></th>
							<th><strong>{{"说明"|T}}</strong></th>
							<th style="width:200px;" class="text-center"><strong>{{"操作"|T}}</strong></th>
						</tr>
					</thead>
					<tbody class="no-border-y">
						{{range $k,$v := Stored.listData}}
						<tr id="collect-p-{{$v.Id}}">
							<td>{{$v.Id}}</td>
							<td>{{$v.Name}}</td>
							<td>{{$v.Browser}}</td>
							<td>{{if $v.Group}}{{$v.Group.Name}}{{else}}{{"无"|T}}{{end}}</td>
							<td>{{(Date $v.Created).Format "2006-01-02 15:04:05"}}</td>
							<td>{{$v.Description}}</td>
							<td class="text-center"><a data-toggle="tooltip" title="{{"加入计划任务"|T}}" class="label label-success" href="__BACKEND__/task/add?command={{">collect_page:"}}{{$v.Id}}"><i class="fa fa-clock-o"></i></a>
							<a class="label label-primary" href="javascript:;" data-toggle="tooltip" title="{{"采集"|T}}" id="collect-start-{{$v.Id}}" data-id="{{$v.Id}}" onclick="return collecting(this,'start')"><i class="fa fa-play"></i></a>
							<a class="label label-danger hidden" href="javascript:;" data-toggle="tooltip" title="{{"停止"|T}}" id="collect-stop-{{$v.Id}}" data-id="{{$v.Id}}" onclick="return collecting(this,'stop')"><i class="fa fa-stop"></i></a>
							<a class="label label-info" data-toggle="tooltip" href="__BACKEND__/collector/rule_collect?id={{$v.Id}}" title="{{"采集测试"|T}}"><i class="fa fa-refresh"></i></a>
							<a class="label label-default" data-toggle="tooltip" href="__BACKEND__/collector/rule_add?copyId={{$v.Id}}" title="{{"复制"|T}}"><i class="fa fa-copy"></i></a>
							<a class="label label-success" data-toggle="tooltip" href="__BACKEND__/collector/rule_edit?id={{$v.Id}}" title="{{"编辑"|T}}"><i class="fa fa-pencil"></i></a> 
							<a class="label label-danger" data-toggle="tooltip" href="__BACKEND__/collector/rule_delete?id={{$v.Id}}" onclick="return confirm('{{"真的要删除吗？"|T}}');" title="{{"删除"|T}}"><i class="fa fa-times"></i></a>
							</td>
						</tr>
                         {{end}}
					</tbody>
				</table>		
				</div>
				{{Stored.pagination.Render}}
			</div>
		</div>				
	</div>
</div>
{{/Block}}
{{Block "footer"}}
<script type="text/template" id="tr-progressbar">
<tr class="tr-progressbar">
	<td colspan="7" style="padding:0">
		<div class="progress-description"></div>
		<div class="progress progress-striped" style="border-radius:0;background-color:#555">
			<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width:0%">
			<span class="sr-only">0%</span>
			</div>
		</div>
	</td>
</tr>
</script>
<script type="text/javascript">
function recv_notice_collector(m){
	var percent=m.progress?m.progress.percent.toFixed(2)*1:0;
    if(m.status>0){
      console.info('['+percent+'/100] '+m.content);
    }else{
      console.error('['+percent+'/100] '+m.content);
	  App.message({title:App.i18n.SYS_INFO,text:m.content,
		time:5000,sticky:false,class_name:'error'});
    }
	if(!m.progress) return;
	var id=m.id;
	var el='collect-p-'+id;
	var tr=$('#'+el);
	if(tr.length<1) return;
	var pb=tr.next('.tr-progressbar');
	if(pb.length<1){
		var html=$('#tr-progressbar').html();
		$('#'+el).after(html);
		pb=$('#'+el).next('.tr-progressbar');
	}
	var bar=pb.children('td').children('.progress').children('.progress-bar');
	bar.css('width',percent+'%').attr('aria-valuenow',percent);
	//bar.children('.sr-only').html('['+percent+'%] '+m.content);
	pb.find('.progress-description').text(' ['+percent+'%] '+m.content);
	if(percent>=100&&m.progress.complete){
		$('#collect-stop-'+id).addClass('hidden');
		$('#collect-start-'+id).removeClass('hidden');
		App.message({title:App.i18n.SYS_INFO,text:m.content,
		time:5000,sticky:false,class_name:'success'});
	}
}
function collecting(a,op){
	var id=$(a).data('id');
	if(op=='start'){
		$(a).addClass('hidden');
		$('#collect-stop-'+id).removeClass('hidden');
	}
	$.post(BACKEND_URL+'/collector/rule_collect',{id:id,op:op,clientID:App.clientID['notify']},function(r){
		if(r.Code<=0){
			op='stop';
		}
		if(op=='stop'){
			$('#collect-stop-'+id).addClass('hidden');
			$('#collect-start-'+id).removeClass('hidden');
			//$('#collect-p-'+id).next('.tr-progressbar').remove();
		}
		App.message({title:App.i18n.SYS_INFO,text:r.Info,time:5000,sticky:false,class_name:r.Code==1?'success':'error'});
	},'json');
}
</script>
{{/Block}}